package com.zzptc.ljl.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.Random;

import master.flame.danmaku.controller.DrawHandler;
import master.flame.danmaku.danmaku.model.BaseDanmaku;
import master.flame.danmaku.danmaku.model.DanmakuTimer;
import master.flame.danmaku.danmaku.model.IDanmakus;
import master.flame.danmaku.danmaku.model.android.DanmakuContext;
import master.flame.danmaku.danmaku.model.android.Danmakus;
import master.flame.danmaku.danmaku.parser.BaseDanmakuParser;
import master.flame.danmaku.ui.widget.DanmakuView;

public class MainActivity extends AppCompatActivity {
    DanmakuView danmakuView;
    LinearLayout linearLayout;
    EditText editText;
    DanmakuContext danmakuContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        event();
    }

    /**
     * 创建弹幕解析器
     */
    private BaseDanmakuParser parser=new BaseDanmakuParser() {
        @Override
        protected IDanmakus parse() {
            return new Danmakus();
        }
    };

    private void event() {

        //准备工作  3个
        danmakuView.prepare(parser,danmakuContext);
        danmakuView.enableDanmakuDrawingCache(true);
        danmakuView.setCallback(new DrawHandler.Callback() {
            @Override
            public void prepared() {
                danmakuView.start();
                suiji();
            }

            @Override
            public void updateTimer(DanmakuTimer timer) {

            }

            @Override
            public void danmakuShown(BaseDanmaku danmaku) {

            }

            @Override
            public void drawingFinished() {

            }
        });


        danmakuView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (linearLayout.getVisibility()==View.GONE){
                    linearLayout.setVisibility(View.VISIBLE);
                }else {
                    linearLayout.setVisibility(View.GONE);
                }
            }
        });
    }

    private void suiji() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                int i = new Random().nextInt(300);
                String str=i+"";
                int j=new Random().nextInt(500);
                try {
                    Thread.sleep(j);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sendDanmu(str);
                }
            }
        }).start();

    }

    private void initView() {
        danmakuView=findViewById(R.id.danmu);
        linearLayout=findViewById(R.id.lin);
        editText=findViewById(R.id.et_sendContent);
        danmakuContext=DanmakuContext.create();
    }

    public void send(View view) {
        Toast.makeText(this, "===", Toast.LENGTH_SHORT).show();
        //让程序健壮
        if (editText.getText().toString().equals("")){
            Toast.makeText(this,"输入框不能为空",Toast.LENGTH_SHORT).show();
        }else {
            String s = editText.getText().toString().trim();
            sendDanmu(s);
            editText.setText("");
        }

    }

    private void sendDanmu(String s) {
        //构造一个弹幕对象
        BaseDanmaku baseDanmaku=danmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_LR);
        baseDanmaku.text=s;
        baseDanmaku.textSize=30;
        baseDanmaku.textColor= Color.RED;
        baseDanmaku.borderColor=Color.BLUE;
        baseDanmaku.setTime(danmakuView.getCurrentTime());


        //添加弹幕
        danmakuView.addDanmaku(baseDanmaku);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (danmakuView!=null&&danmakuView.isPrepared()&&danmakuView.isPaused()){
            danmakuView.release();//中断后继续
        }
    }

    /**
     * 暂停
     */
    @Override
    protected void onPause() {
        super.onPause();
        if (danmakuView!=null&&danmakuView.isPrepared()){
            danmakuView.pause();
        }
    }
    /*
  *销毁
 */


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (danmakuView!=null){
            danmakuView.release();
            danmakuView=null;
        }
    }
}
